//
//  MLVisitorView.swift
//  weibo
//
//  Created by manmanlang on 16/6/22.
//  Copyright © 2016年 manmanlang. All rights reserved.
//

import UIKit

///必须继承NSObjectProtocol才可以使用weak 代理声明
protocol MLVisitorViewDelegate:NSObjectProtocol {
    
    func leftBarButtonClick()
    func rightBarButtonClick()
}

class MLVisitorView: UIView {
    
    weak var delegate:MLVisitorViewDelegate?
    
    ///设置信息
    func setupInfo(message:String?, image:String?) {
        //通过设置图片为可选项来判断是否需要显示小房子
        if let Oimage = image {
            animationView.hidden = true;
            houseView.image = UIImage(named:Oimage)
        }else {
            animationView.hidden = false
        }
        messageLabel.text = message
    }
    
    ///动画方法
    func startAnimation() {
        
        let animation = CABasicAnimation(keyPath:"transform.rotation")
        animation.toValue = 2 * M_PI
        animation.repeatCount = MAXFLOAT
        animation.duration = 20
        //当完成动画或者视图到后台的时候会默认移除动画，而我不想移除它
        animation.removedOnCompletion = false;
        animationView.layer.addAnimation(animation, forKey: nil)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func leftBarButtonClick() {
        printLog("注册")
        delegate?.leftBarButtonClick()
    }
    func rightBarButtonClick() {
        printLog("登录")
        leftBarButtonClick()
        delegate?.rightBarButtonClick()
    }
    
    ///遮盖视图
    private lazy var setupVisitorView:UIImageView = {
    
        let visitorView = UIImageView()
        visitorView.image = UIImage(named:"visitordiscover_feed_mask_smallicon")
        return visitorView
    
    }()
    
    ///首页的小房子
    private lazy var houseView:UIImageView = {
        
        let houseView = UIImageView()
        houseView.image = UIImage(named:"visitordiscover_feed_image_house")
        return houseView
        
    }()
    
    ///提示语
    private lazy var messageLabel:UILabel = {
        
        let messageLabel = UILabel()
        messageLabel.textColor = UIColor.darkGrayColor()
        messageLabel.textAlignment = .Center
        messageLabel.numberOfLines = 0
        return messageLabel
    }()
    
    ///注册按钮
    private lazy var registerButton: UIButton = {
        
        let registerButton = UIButton(type: UIButtonType.Custom)
        
        registerButton.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        registerButton.setTitle("注册", forState: UIControlState.Normal)
        registerButton.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        
        registerButton.addTarget(self, action: #selector(MLVisitorView.leftBarButtonClick), forControlEvents: UIControlEvents.TouchUpInside)
        
        return registerButton
        
    }()
    
    ///登录按钮
    private lazy var loginButton: UIButton = {
        
        let loginButton = UIButton(type: UIButtonType.Custom)
        
        loginButton.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        loginButton.setTitle("登录", forState: UIControlState.Normal)
        loginButton.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        
        loginButton.addTarget(self, action: #selector(MLVisitorView.rightBarButtonClick), forControlEvents: UIControlEvents.TouchUpInside)
        
        return loginButton
        
    }()
    
    private var animationView:UIImageView = {
        
        let animationView = UIImageView()
        
        animationView.image = UIImage(named:"visitordiscover_feed_image_smallicon")
        
        animationView.sizeToFit()
        
        return animationView
        
    }()
    
}

///进行代码分块 代码块内不能存在属性
extension MLVisitorView {
    
    private func setUI() {
        
        backgroundColor = UIColor(white: 237/255.0, alpha: 1.0)
        
        addSubview(animationView)
        addSubview(setupVisitorView)
        addSubview(houseView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        ///设置约束
        animationView.translatesAutoresizingMaskIntoConstraints = false
        setupVisitorView.translatesAutoresizingMaskIntoConstraints = false
        houseView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        
        // 手动添加约束
        /*
         item: <#T##AnyObject#>,  -- 要给哪个控件设置约束
         attribute: <#T##NSLayoutAttribute#>,  -- 设置对齐的方向
         relatedBy: <#T##NSLayoutRelation#>,  有三个选项,我们一般选用 equal
         toItem: <#T##AnyObject?#>,  -- 参照于哪个试图
         attribute: <#T##NSLayoutAttribute#>,
         multiplier: <#T##CGFloat#>,  系数 0~1
         constant: <#T##CGFloat#>    微调值
         
         Create constraints explicitly.  Constraints are of the form "view1.attr1 = view2.attr2 * multiplier + constant
         */
        addConstraint(NSLayoutConstraint(item: animationView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: animationView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        // 遮罩试图的约束 -- 先让他和 父试图,宽高相同,然后水平居中,垂直居中
        addConstraint(NSLayoutConstraint(item: setupVisitorView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: setupVisitorView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: setupVisitorView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: 0))
        
        addConstraint(NSLayoutConstraint(item: setupVisitorView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Height, multiplier: 1.0, constant: -350))
        
        //首页的小房子
        addConstraint(NSLayoutConstraint(item: houseView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: houseView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        
        //提示label --
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: animationView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 10))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 224))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        
        // 注册按钮 -- 宽 100,高 30 , 左边和 messageLabel的左边对其,顶部和其底部对齐
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 30))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 10))
        
        
        
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 30))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 10))
    }
    
}








